//
//  YWEditController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/25.
//

#import "YWEditController.h"
#import "YWDrawView.h"
#import "YWGraffitiController.h"

#import "YWMosaicController.h"
#import "YWFaceController.h"
#import "YWInsertPhotoController.h"
#import "YWInsertTextController.h"
#import "YWToningController.h"
#import "YWCoreImageController.h"
#import "YWTailorController.h"


@interface YWEditController ()<YWGraffitiControllerDelegate,YWMosaicControllerDelegate,YWFaceControllerDelegate,YWInsertPhotoControllerDelegate,YWInsertTextControllerDelegate,YWToningControllerDelegate,YWCoreImageControllerDelegate,YWTailorControllerDelegate>


@property (nonatomic, strong) NSMutableArray *sequenceSizeArray; //顺序数组

@property (nonatomic, strong) NSMutableArray *showSizeArray; //显示数组

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) YWDrawView *drawView;

@property(nonatomic,assign)BOOL isAlreadyAppear; //是否第次进来
- (IBAction)graffitiClick:(UIButton *)sender;
- (IBAction)dismissClick:(UIButton *)sender;
- (IBAction)faceClick:(UIButton *)sender;
- (IBAction)mosaicClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *compareBtn;

- (IBAction)previousBtnClick:(UIButton *)sender;

- (IBAction)nextBtnClick:(UIButton *)sender;

- (IBAction)removeAllBtnClick:(UIButton *)sender;

- (IBAction)conformAndSaveBtnClick:(UIButton *)sender;

- (IBAction)insertPhotoBtnClick:(UIButton *)sender;

- (IBAction)insertTextBtnClick:(UIButton *)sender;

- (IBAction)toningBtnClick:(UIButton *)sender;

- (IBAction)coreImageBtnClick:(UIButton *)sender;

- (IBAction)tailorBtnClick:(UIButton *)sender;

@end

@implementation YWEditController

//self.drawView的数组装image、editVC的数组装image的size、因为它有个裁剪功能所以我VC不得不对image的size进行处理
- (NSMutableArray *)sequenceSizeArray {
    
    if (_sequenceSizeArray == nil) {
        _sequenceSizeArray = [NSMutableArray array];
    }
    return _sequenceSizeArray;
}

- (NSMutableArray *)showSizeArray {
    
    if (_showSizeArray == nil) {
        _showSizeArray = [NSMutableArray array];
    }
    return _showSizeArray;
}


//添加图层Frame
-(void)addImageSize:(CGSize)size{
    
    NSValue *sizeValue = @(size);
    
    //从顺序数组截取显示数组的数量、然后才添加到顺序数组
    self.sequenceSizeArray = [NSMutableArray arrayWithArray:[self.sequenceSizeArray subarrayWithRange:NSMakeRange(0, self.showSizeArray.count)]];
    
    [self.sequenceSizeArray addObject:sizeValue];
    
    //然后显示数组再从顺序数组获取同等数据
    self.showSizeArray = [NSMutableArray arrayWithArray:self.sequenceSizeArray];
    
    [self setNeedsDisplaySize:size];
    
}

//撤销图层 (不要动顺序数组、只要动显示数组)
-(void)previousHandleSize{
    
    if (self.showSizeArray.count > 1) {
        
        [self.showSizeArray removeLastObject];
        
        NSValue *lastSizeValue = self.showSizeArray.lastObject;
        [self setNeedsDisplaySize:lastSizeValue.CGSizeValue];
    }
    
}

//前进图层 (不要动顺序数组、只要动显示数组)
-(void)nextHandleSize{
    
    if (self.showSizeArray.count < self.sequenceSizeArray.count) {
        
        NSValue *nextValue = self.sequenceSizeArray[self.showSizeArray.count];
        
        [self.showSizeArray addObject:nextValue];
        
        NSValue *lastSizeValue = self.showSizeArray.lastObject;
        [self setNeedsDisplaySize:lastSizeValue.CGSizeValue];
    }
    
}


//重置图层
-(void)removeAllHandleSize{
    
    if (self.sequenceSizeArray.count > 0) {
        
        NSValue *firstValue = self.sequenceSizeArray[0];
        //删剩最后一个模型就好
        [self.sequenceSizeArray removeAllObjects];
        self.sequenceSizeArray = [NSMutableArray arrayWithArray:@[firstValue]];
        
        //然后显示数组再从顺序数组获取同等数据
        self.showSizeArray = [NSMutableArray arrayWithArray:self.sequenceSizeArray];
        
        NSValue *lastSizeValue = self.showSizeArray.lastObject;
        [self setNeedsDisplaySize:lastSizeValue.CGSizeValue];
    }
    
}

-(void)setNeedsDisplaySize:(CGSize)size{
    
    MJWeakSelf
    CGFloat w = self.bgView.frame.size.width;
    CGFloat h = w * size.height*1.0/size.width;
    CGFloat y = 0;
    CGFloat x = 0;
    if (h > self.bgView.frame.size.height) {
        h = self.bgView.frame.size.height;
        y = 0;
        w = h * size.width*1.0/size.height;
        x = (self.bgView.frame.size.width - w)*1.0/2;
        
        [self.drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
               make.left.equalTo(weakSelf.drawView.superview).with.offset(x);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(-x);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.top.equalTo(weakSelf.drawView.superview).with.offset(0);
        }];
        
        
        
    }
    else{
        x = 0;
        w = self.bgView.frame.size.width;
        h = w * size.height*1.0/size.width;
        y = (self.bgView.frame.size.height - h)*0.5;
        
        [self.drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
               make.left.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(0);
                make.top.equalTo(weakSelf.drawView.superview).with.offset(y);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(-y);
        }];
        
    }
    
    
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    
    //给imageView原图
    self.imageView.image = self.image;
    
    //创建画view并添加到bgView
    YWDrawView *drawView = [[YWDrawView alloc] init];
    self.drawView = drawView;
    [self.bgView addSubview:drawView];
    
    UILongPressGestureRecognizer *longPressForCompare = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressForCompare:)];
    longPressForCompare.minimumPressDuration = 0;
    [self.compareBtn addGestureRecognizer:longPressForCompare];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    //显示第一张底图层
    if (self.isAlreadyAppear == NO){
        
        self.isAlreadyAppear = YES;
        
        self.imageView.hidden = YES; //不是对比的话一开始imageView就要隐藏
        
        NSLog(@"===%f,%f",self.image.size.width,self.image.size.height);
        
        [self addImageSize:self.image.size];
        
        [self.drawView addImage:self.image withType:YWLayerStatusType_firstImage];
        
        
    }
    
    
}


- (IBAction)tailorBtnClick:(UIButton *)sender {
    
    YWTailorController *vc = [[YWTailorController alloc] initWithNibName:@"YWTailorController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}

-(void)tailorController:(YWTailorController *)tailorController newImage:(UIImage *)newImage{
 
    [self addImageSize:newImage.size];
    
    [self.drawView addImage:newImage withType:YWLayerStatusType_Tailor];
}

- (IBAction)coreImageBtnClick:(UIButton *)sender {
    
    YWCoreImageController *vc = [[YWCoreImageController alloc] initWithNibName:@"YWCoreImageController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}

-(void)coreImageController:(YWCoreImageController *)coreImageController newImage:(UIImage *)newImage{
    
    [self addImageSize:newImage.size];
    
    [self.drawView addImage:newImage withType:YWLayerStatusType_CoreImage];
}

- (IBAction)toningBtnClick:(UIButton *)sender {
    
    YWToningController *vc = [[YWToningController alloc] initWithNibName:@"YWToningController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
    
}

-(void)toningController:(YWToningController *)toningController newImage:(UIImage *)newImage{
    
    [self addImageSize:newImage.size];
       
    [self.drawView addImage:newImage withType:YWLayerStatusType_Toning];
    
}

- (IBAction)insertTextBtnClick:(UIButton *)sender {
    
    YWInsertTextController *vc = [[YWInsertTextController alloc] initWithNibName:@"YWInsertTextController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
    
}

-(void)insertTextController:(YWInsertTextController *)insertTextController newImage:(UIImage *)newImage{
    
    
    [self addImageSize:newImage.size];
       
    [self.drawView addImage:newImage withType:YWLayerStatusType_InsertText];
    
}

- (IBAction)insertPhotoBtnClick:(UIButton *)sender {
    YWInsertPhotoController *vc = [[YWInsertPhotoController alloc] initWithNibName:@"YWInsertPhotoController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}

-(void)insertPhotoController:(YWInsertPhotoController *)insertPhotoController newImage:(UIImage *)newImage{
    
    
    [self addImageSize:newImage.size];
       
       
    [self.drawView addImage:newImage withType:YWLayerStatusType_InsertPhoto];
}

- (IBAction)conformAndSaveBtnClick:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(editController:newImage:)]){
        [self.delegate editController:self newImage:[self.drawView getCurrentLayer]];
    }
    //把图片保存到系统相册当中
    //注意:@selector里面的方法不能够瞎写,必须得是image:didFinishSavingWithError:contextInfo:
    UIImageWriteToSavedPhotosAlbum([self.drawView getCurrentLayer], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//保存完毕时调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSLog(@"success");
    
}

-(void)longPressForCompare:(UILongPressGestureRecognizer *)longPress{
    
    if(longPress.state == UIGestureRecognizerStateBegan){
        self.drawView.hidden = YES;
        self.imageView.hidden = NO;
    }
    else if(longPress.state == UIGestureRecognizerStateEnded){
        self.drawView.hidden = NO;
        self.imageView.hidden = YES;
    }
    
}

- (IBAction)removeAllBtnClick:(UIButton *)sender {
    
    [self removeAllHandleSize];
    
    [self.drawView removeAllHandle];
}

- (IBAction)nextBtnClick:(UIButton *)sender {
    
    [self nextHandleSize];
    
    [self.drawView nextHandle];
}

- (IBAction)previousBtnClick:(UIButton *)sender {
    
    [self previousHandleSize];
    
    [self.drawView previousHandle];
}

- (IBAction)mosaicClick:(UIButton *)sender {
    
    YWMosaicController *vc = [[YWMosaicController alloc] initWithNibName:@"YWMosaicController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}

- (IBAction)dismissClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//去涂鸦
- (IBAction)graffitiClick:(UIButton *)sender {
    
    YWGraffitiController *vc = [[YWGraffitiController alloc] initWithNibName:@"YWGraffitiController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
    
}
-(void)graffitiController:(YWGraffitiController *)graffitiController newImage:(UIImage *)newImage{
    
    
    [self addImageSize:newImage.size];
       
    [self.drawView addImage:newImage withType:YWLayerStatusType_Graffiti];
    
}

-(void)mosaicController:(YWMosaicController *)mosaicController newImage:(UIImage *)newImage{
    
    
    [self addImageSize:newImage.size];
    
    [self.drawView addImage:newImage withType:YWLayerStatusType_Mosaic];
}

- (IBAction)faceClick:(UIButton *)sender {
    YWFaceController *vc = [[YWFaceController alloc] initWithNibName:@"YWFaceController" bundle:nil];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.image = [self.drawView getCurrentLayer];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}

-(void)faceController:(YWFaceController *)faceController newImage:(UIImage *)newImage{
    
    [self addImageSize:newImage.size];
       
    
    [self.drawView addImage:newImage withType:YWLayerStatusType_Face];
}




@end
